'use strict'
const {getOrderNo} = require('../../commons/util.js');
const crypto = require('crypto');
const allcConfig   = require('config')
const config = allcConfig.get('Customer.ossConfig')



/**
 * 初始化
 */
class OssService {
    constructor() {

    }

    async getOssConfig(){
        const dirPath = 'post/' 
        const {OSSAccessKeyId, host, secret} = config
        let end = new Date().getTime() + 300000
        let expiration = new Date(end).toISOString()
        let policyString = {
            expiration,
            conditions: [
              ["content-length-range", 0, 1048576000],
              ["starts-with", "$key", dirPath]
            ]
        }
        policyString = JSON.stringify(policyString)
        const policy = new Buffer(policyString).toString('base64')
        const signature = crypto.createHmac('sha1', secret).update(policy).digest('base64')
        let res = {
            OSSAccessKeyId: OSSAccessKeyId,
            host,
            policy,
            signature,
            saveName: getOrderNo(),
            startsWith: dirPath
        }
        return res;
    }

}

module.exports = new OssService()